package com.sangu.dianxiu.fragment;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.sangu.dianxiu.BaseApp;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.R;
import com.sangu.dianxiu.bean.user.LogOutEntity;
import com.sangu.dianxiu.bean.user.User;
import com.sangu.dianxiu.config.Constants;
import com.sangu.dianxiu.ui.AboutActivity;
import com.sangu.dianxiu.ui.FeedbackActivity;
import com.sangu.dianxiu.ui.FrequentAddressActivity;
import com.sangu.dianxiu.ui.FrequentContactsActivity;
import com.sangu.dianxiu.ui.LoginActivity;
import com.sangu.dianxiu.utils.FragmentTabUtils;
import com.sangu.dianxiu.utils.UserManager;
import com.sangu.dianxiu.widget.CustomDialog;

import java.util.HashMap;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends Fragment {
    UserManager userManager=new UserManager();      //使用UserManager中的方法进行网络请求
    @ViewInject(R.id.img_fragment_mine_userface)    //使用XUtils的注解框架，这样就不用findViewById了
    private SimpleDraweeView ivUserface;
    @ViewInject(R.id.tv_fragment_mine_unlogin)
    private TextView tvUnlogin;
    @ViewInject(R.id.tv_fragment_mine_username)
    private TextView tvUserName;
    @ViewInject(R.id.tv_fragment_mine_usephone)
    private TextView tvUserPhone;
    @ViewInject(R.id.tv_fragment_mine_contact)
    private TextView tvContact;
    @ViewInject(R.id.tv_fragment_mine_address)
    private TextView tvAddress;
    @ViewInject(R.id.tv_fragment_mine_score_exchange)
    private TextView tvScoreExchange;
    @ViewInject(R.id.tv_fragment_mine_enterprise)
    private TextView tvEnterprise;
    @ViewInject(R.id.tv_fragment_mine_call)
    private TextView tvCall;
    @ViewInject(R.id.tv_fragment_mine_recommend)
    private TextView tvRecommend;
    @ViewInject(R.id.tv_fragment_mine_about)
    private TextView tvAbout;
    @ViewInject(R.id.btn_fragment_mine_exit)
    private Button btnExit;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_mine, container, false);
        ViewUtils.inject(this, view);           //如果没有这一句则上面的注解不会生效
        init();
        return view;
    }

    private void init() {
        if(BaseApp.isLogin)
        {
            //如果已经处于登录状态
            //从SharedPreferences中读取用户的手机号码等信息，并显示出来
            SharedPreferences preferences=getActivity().getSharedPreferences(Constants.IS_FIRST_RUN, Context.MODE_PRIVATE);
            String phoneNumber=preferences.getString(Constants.PHONE_NUMBER,"");
            tvUnlogin.setVisibility(View.GONE);                     //登录后调整相应的我的界面的显示
            tvUserName.setVisibility(View.VISIBLE);
            tvUserPhone.setVisibility(View.VISIBLE);
            btnExit.setVisibility(View.VISIBLE);
            tvUserName.setText("罗志磊");
            tvUserPhone.setText(phoneNumber);
            btnExit.setText("退出登录");
        }

    }

    //使用XUtils的事件监听注解，这样就不用setOnClickListener了
    @OnClick({R.id.img_fragment_mine_userface,R.id.tv_fragment_mine_unlogin,R.id.tv_fragment_mine_username,R.id.tv_fragment_mine_usephone,
            R.id.tv_fragment_mine_contact,R.id.tv_fragment_mine_address,R.id.tv_fragment_mine_score_exchange,R.id.tv_fragment_mine_enterprise,
            R.id.tv_fragment_mine_call,R.id.tv_fragment_mine_feedback,R.id.tv_fragment_mine_recommend,R.id.tv_fragment_mine_about,R.id.btn_fragment_mine_exit})
    public void onClick(View v)
    {
        //如果是点击客服和关于，就不用登录了，若点击其他的，没有登录的话，则进入登录界面
        Intent intent;
        if(!BaseApp.isLogin&&(v.getId()!=R.id.tv_fragment_mine_call)&&(v.getId()!=R.id.tv_fragment_mine_about))
        {
            login();
        }
        else
        {
            switch (v.getId())
            {
                case R.id.img_fragment_mine_userface:

                    break;
                case R.id.tv_fragment_mine_unlogin:

                    break;

                case R.id.tv_fragment_mine_username:

                    break;
                case R.id.tv_fragment_mine_usephone:

                    break;
                case R.id.tv_fragment_mine_contact:
                    intent=new Intent(getActivity(), FrequentContactsActivity.class);           //调到联系人Activity
                    startActivity(intent);
                    break;
                case R.id.tv_fragment_mine_address:
                    intent=new Intent(getActivity(), FrequentAddressActivity.class);            //跳到常用地址Activity
                    startActivity(intent);
                    break;
                case R.id.tv_fragment_mine_score_exchange:
                    ((RadioButton) (FragmentTabUtils.rgs.getChildAt(1))).setChecked(true);      //用此方法切换到积分商城fragment
                    break;
                case R.id.tv_fragment_mine_enterprise:
                    CustomDialog dialog = new CustomDialog(getActivity(), R.style.mystyle,
                            R.layout.add_enterprise_maintain_dialog);
                    dialog.show();
                    break;
                case R.id.tv_fragment_mine_call:                                                //调用系统的打电话界面
                    intent=new Intent();
                    intent.setAction(Intent.ACTION_DIAL);
                    String tel="tel:"+tvCall.getText().toString();
                    Uri uri=Uri.parse(tel);
                    intent.setData(uri);
                    startActivity(intent);
                    break;
                case R.id.tv_fragment_mine_feedback:                                            //跳到用户反馈Activity
                    intent=new Intent(getActivity(), FeedbackActivity.class);
                    startActivity(intent);
                    break;
                case R.id.tv_fragment_mine_recommend:                                           //推荐目前还没有确定，后期要补上

                    break;
                case R.id.tv_fragment_mine_about:                                               //跳到关于activity
                    intent=new Intent(getActivity(),AboutActivity.class);
                    startActivity(intent);
                    break;
                case R.id.btn_fragment_mine_exit:                                               //退出登录
                    logout();
                    break;
            }
        }
    }
    //退出登录
    private void logout() {
        HashMap<String,String> map=new HashMap<>();
        map.put("phone",tvUserPhone.getText().toString().trim());
        userManager.logout(map, new ICallBack() {
            @Override
            public void onSuccess(String string) {
                LogOutEntity entity= JSONObject.parseObject(string,LogOutEntity.class);
                if(entity.getRc()==0)
                {
                    BaseApp.isLogin=false;                                  //退出登录后要调整相应的我的界面
                    tvUnlogin.setVisibility(View.VISIBLE);
                    tvUserName.setVisibility(View.GONE);
                    tvUserPhone.setVisibility(View.GONE);
                    btnExit.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        });
    }

    //当登陆成功后，会回调此方法
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==Constants.REQUEST_CODE_LOGIN) {
            if(data!=null)
            {
                tvUnlogin.setVisibility(View.GONE);
                tvUserName.setVisibility(View.VISIBLE);
                tvUserPhone.setVisibility(View.VISIBLE);
                btnExit.setVisibility(View.VISIBLE);
                tvUserPhone.setText(BaseApp.user.getUserPhone());
                btnExit.setText("退出登录");
                BaseApp.isLogin=true;               //登录成功后要将登录登录状态改为已经登录
            }
        }
    }

    //跳转到登录界面
    private void login() {
        Intent intent=new Intent(getActivity(),LoginActivity.class);
        startActivityForResult(intent, Constants.REQUEST_CODE_LOGIN);
    }

    //在Activity销毁的时候取消volley框架的网络请求，避免消耗多余的流量
    @Override
    public void onDestroy() {
        super.onDestroy();
        userManager.requestQueue.cancelAll(UserManager.class);
    }
}
